﻿@using LunchCommand.Models
@using LunchCommand.ViewModels

@model LunchCommandViewModel
@{
    ViewBag.Title = "";
}

@section styles{
    @Styles.Render("~/Content/Home.css")
}
@using (Html.BeginForm())
{
    <div class="jumbotron">
        <h1 class="lunch-status">Loading...</h1>
        <div class="after-decision" style="display: none">
            <h1 id="mainVenue" class="venue-name">Cheeseburger Bobby's</h1>
            <h3 style="text-align:center"> Consensus: <span id="mainConsensus">100%</span></h3>
            <h3 id="travelTime" class="text-info" style="text-align:center"> Travel Time: </h3>

            <br />
            <div class="quick-venue">
                <h3>Those in a hurry can go to <span id="quickVenue">Arby's</span> <span class="text-muted consensus" id="quickConsensus">(85% Consensus)</span></h3>
            </div>
            <div class="dissenting-venue">
                <h3>Dissenters can go to <span id="dissentingVenue">Panera Bread</span> <span class="text-muted consensus" id="dissentingConsensus">(34% Consensus)</span></h3>
            </div>
        </div>
        <p class="before-decision" style="display:none">Lunch will be decided in <span id="timeUntilLunch" class="text-info">10:00</span></p>
    </div>

    <div class="row">
        @Html.HiddenFor(m => m.UserHasDecidedToday, htmlAttributes: new { id = "hasDecided" })
        @Html.HiddenFor(m => m.UserIsGoingToday, htmlAttributes: new { id = "isGoing" })
        @Html.HiddenFor(m => m.VenueChoicesMade, htmlAttributes: new { id = "venuesChanged" })

        <div class="question">

            <script>
                submit = function (going) {
                    $('#hasDecided').val(true);
                    $('#isGoing').val("" + going);
                    $("#venuesChanged").val("false");
                }
            </script>

            @if (Model.TodaysLunch.LunchTime > DateTime.Now)
            {

                if (!Model.UserHasDecidedToday)
                {
                    <h2>Are you going to lunch today?</h2>
                    <div class="centered">
                        <input type="submit" class="btn btn-success" onclick="window.submit(true);" value="Yes" />
                        <input type="submit" class="btn btn-danger" onclick="window.submit(false);" value="No" />
                    </div>
                }
                else if (!Model.UserIsGoingToday)
                {
                    <h2 class="text-danger">You are not going to lunch today</h2>
                    <div class="centered">
                        <input type="submit" class="btn btn-danger" onclick="window.submit(true);" value="Change Mind" />
                    </div>
                }
                else
                {
                    <h2 class="text-success">You are going to lunch today</h2>
                    <div class="centered">
                        <input type="submit" class="btn btn-success" onclick="window.submit(false);" value="Change Mind" />
                    </div>
                }
            }
        </div>
        <hr />
        <div class="col-md-5">
            <h2>Update your lunch preferences</h2>
            <h5>Remember to click save at the bottom.</h5>
            @if (Model.AnyVenuesNew)
            {
                <h4>There are <b class="text-danger">new</b> venues for you to choose</h4>
            }
            <table class="table">

                @{
                    var yesTooltip = "You want to go here";
                    var maybeTooltip = "You will go here only if others want to go here";
                    var noTooltip = "You refuse to go here";
                }

                <thead>
                    <tr>
                        <td class="radio-col" title="@yesTooltip">
                            <span class="glyphicon glyphicon-ok text-success"></span>
                        </td>
                        <td class="radio-col" title="@maybeTooltip">
                            <span class="glyphicon glyphicon-minus"></span>
                        </td>
                        <td class="radio-col" title="@noTooltip">
                            <span class="glyphicon glyphicon-remove text-danger"></span>
                        </td>
                    </tr>
                </thead>

                @{var venues = Model.VenueChoicesForUser.ToArray();}
                @for (int i = 0; i < Model.VenueChoicesForUser.Count(); i++)
                {
                    var venue = venues[i];
                    var radioName = String.Format("VenueChoicesForUser[{0}].Preference", Array.IndexOf(venues, venue));
                    var hidden1Name = String.Format("VenueChoicesForUser[{0}].VenueID", Array.IndexOf(venues, venue));
                    var hidden2Name = String.Format("VenueChoicesForUser[{0}].VenueName", Array.IndexOf(venues, venue));

                    <tr>
                        <td class="radio-col" title="@yesTooltip">
                            @Html.RadioButton(radioName, DegreeOfPreference.WantToGo, isChecked: venue.Preference == DegreeOfPreference.WantToGo, htmlAttributes: new { id = radioName + "_yes", @class = "form-control" })

                        </td>
                        <td class="radio-col" title="@maybeTooltip">
                            @Html.RadioButton(radioName, DegreeOfPreference.WillGo, isChecked: venue.Preference == DegreeOfPreference.WillGo, htmlAttributes: new { id = radioName + "_maybe", @class = "form-control" })

                        </td>
                        <td class="radio-col" title="@noTooltip">
                            @Html.RadioButton(radioName, DegreeOfPreference.WillNotGo, isChecked: venue.Preference == DegreeOfPreference.WillNotGo, htmlAttributes: new { id = radioName + "_no", @class = "form-control" })
                        </td>
                        <td>
                            <input type="hidden" name="@hidden1Name" value="@venue.VenueId" />
                            <input type="hidden" name="@hidden2Name" value="@venue.VenueName" />

                            <label class="form-label @(venue.IsNew? "new-venue text-danger" : "")">@venue.VenueName</label>

                        </td>
                    </tr>
                }
            </table>
            <div class="center-content">
                <input type="submit" class="btn btn-primary" onclick="$('#venuesChanged').val('true')" value="Save Preferences" />
                <a class="btn btn-default" href="@Url.Action("Index", "Venues")">Add/Edit Venues</a>
            </div>
        </div>
        <div class="col-md-3">
            <h2>Lunchers Today</h2>
            @{var lunchersToday = Model.UsersToday.Count(u => u.Item2.GetValueOrDefault(false));}
            <h4>@lunchersToday @(lunchersToday == 1 ? "person is" : "people are") going today</h4>
            <table class="table users-table">
                @foreach (var user in Model.UsersToday)
                {
                    <tr>
                        <td class="check">
                            @if (!user.Item2.HasValue)
                            {
                                <span class="glyphicon glyphicon-minus"></span>
                            }
                            else if (user.Item2.Value)
                            {
                                <span class="glyphicon glyphicon-ok text-success"></span>
                            }
                            else
                            {
                                <span class="glyphicon glyphicon-remove text-danger"></span>
                            }
                        </td>

                        <td>
                            @user.Item1
                        </td>
                    </tr>
                }
            </table>

        </div>
        <div class="col-md-4">
            <h2>Today's Possible Venues</h2>
            <table class="table">
                @{
                    var possibilities = Model.PossibleVenues;
                }
                @foreach (var v in possibilities)
                {
                    <tr>
                        <td>
                            @v.Name
                        </td>
                        <td>
                            @(v.LastVisitedString.Equals("Never") ? "" : v.LastVisitedString)
                        </td>
                        <td>
                            @(v.IsQuick ? "(Quick)" : "")
                        </td>
                    </tr>
                }
            </table>
            @if (possibilities.Count() == 0)
            {
                if (Model.UsersToday.Any(ugt => ugt.Item2.GetValueOrDefault(false)))
                {
                    <h3 class="text-danger">No consensus can be determined.  This can happen if all agreed-upon venues have been visited too recently (less than a week by default).  Check the settings for venues <a href="@Url.Action("Index", "Venues")">here.</a></h3>
                }
                else
                {
                    <h3 class="text-danger">There will not be a lunch today because no one has decided to go.</h3>
                }
            }

        </div>
    </div>
                    }
@section scripts
    {
    <script>
        var controller = new LunchCommand.LunchStatusController($('.jumbotron'));
        controller.initialize();
    </script>
}